﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Apex.Common.Base.ViewModels;
using Apex.Common.MVVM;
using Apex.Script.Models;

namespace Apex.Script.ViewModels
{
    public class ScriptViewModel : CollectionViewModel<Script.Models.Script, SequenceGroupViewModel, SequenceGroup>
    {
        public int ID
        {
            get { return Model.ID; }
            set { Model.ID = value; OnPropertyChanged("ID"); }
        }

        public string Name
        {
            get { return Model.Name; }
            set { Model.Name = value; OnPropertyChanged("Name"); }
        }

        public string Description
        {
            get { return Model.Description; }
            set { Model.Description = value; OnPropertyChanged("Description"); }
        }

        public string Author
        {
            get { return Model.Author; }
            set { Model.Author = value; OnPropertyChanged("Author"); }
        }

        public int Version
        {
            get { return Model.Version; }
            set { Model.Version = value; OnPropertyChanged("Version"); }
        }

        public string ProductionCompany
        {
            get { return Model.ProductionCompany; }
            set { Model.ProductionCompany = value; OnPropertyChanged("ProductionCompany"); }
        }

        public ObservableCollection<LocationViewModel> Locations { get; set; }

        public string Group { get; set; }

        public string TreeDisplayText
        {
            get { return Name; }
        }

        public ScriptViewModel()
            : base()
        {
            Group = "Sequence Groups";
        }

        public ScriptViewModel(Models.Script model)
            : base(model)
        {
            Group = "Sequence Groups";
            _items.CollectionChanged -= _items_CollectionChanged;
            foreach (var x in model.Items)
            {
                _items.Add(new SequenceGroupViewModel(x));
            }
            _items.CollectionChanged += _items_CollectionChanged;

            Locations = new ObservableCollection<LocationViewModel>();
            foreach (var x in model.Locations)
            {
                Locations.Add(new LocationViewModel(x));
            }
            Locations.CollectionChanged += Locations_CollectionChanged;
        }

        void Locations_CollectionChanged(object sender, System.Collections.Specialized.NotifyCollectionChangedEventArgs e)
        {
            switch (e.Action)
            {
                case System.Collections.Specialized.NotifyCollectionChangedAction.Add:
                    foreach (LocationViewModel x in e.NewItems)
                    {
                        Model.Locations.Add(x.Model);
                    }
                    break;
                case System.Collections.Specialized.NotifyCollectionChangedAction.Remove:
                    foreach (LocationViewModel x in e.OldItems)
                    {
                        Model.Locations.Remove(x.Model);
                    }
                    break;
                case System.Collections.Specialized.NotifyCollectionChangedAction.Move:
                case System.Collections.Specialized.NotifyCollectionChangedAction.Replace:
                case System.Collections.Specialized.NotifyCollectionChangedAction.Reset:
                default:
                    break;
            }
        }
    }
}
